package com.imstlife.vending.net;

import android.text.TextUtils;


import com.imstlife.vending.utils.SSLSocketFactoryUtils;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

import io.reactivex.annotations.NonNull;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.HttpException;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by wen on 2017/9/1.
 */

public class RetrofitManager {

    //    private static final String TAG = "RetrofitManager";
    // 网络请求超时时间
    private static final int connectTimeout = 6;
    private static final int readTimeout = 6;
    private static final int writeTimeout = 6;

    private static final String baseUrl = "http://www.imstlife.com.cn/MarvSmart/locker/";

    private static Retrofit gsonRetrofit;
    private static Retrofit gsonRetrofit2;

    private static Retrofit getGsonRetrofit() {
        if (gsonRetrofit == null) {

            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

                @Override
                public void log(String message) {
                    //打印retrofit日志
                    if ((message != null && message.length() >= 5 && !message.contains(": ")
                            && !message.contains("END "))
                            || message.contains("<-- HTTP")) {
                    }
                }
            });
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            Interceptor header = new Interceptor() {

                @Override

                public Response intercept(Chain chain) throws IOException {

                    Request request = chain.request()

                            .newBuilder()

                            //                            .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                            //
                            //                            .addHeader("Accept-Encoding", "gzip, deflate")

                            .addHeader("Connection", "close")

                            //                            .addHeader("Accept", "*/*")
                            //
                            //                            .addHeader("Cookie", "add cookies here")

                            .build();

                    return chain.proceed(request);

                }


            };

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(connectTimeout, TimeUnit.SECONDS)
                    .readTimeout(readTimeout, TimeUnit.SECONDS)
                    .writeTimeout(writeTimeout, TimeUnit.SECONDS)
                    .sslSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory(), SSLSocketFactoryUtils.createTrustAllManager())
                    .hostnameVerifier(new SSLSocketFactoryUtils.TrustAllHostnameVerifier())
                    .addInterceptor(loggingInterceptor)
                    .addInterceptor(header)
                    .build();

            gsonRetrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())//添加gson转换器
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
                    .client(okHttpClient)
                    .build();

        }
        return gsonRetrofit;
    }

    public static <T> T createGsonService(Class<T> service) {
        return getGsonRetrofit().create(service);
    }



    private static Retrofit getJsonRetrofit() {
        if (gsonRetrofit2 == null) {

            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

                @Override
                public void log(String message) {
                    //打印retrofit日志
                    if ((message != null && message.length() >= 5 && !message.contains(": ")
                            && !message.contains("END "))
                            || message.contains("<-- HTTP")) {
                    }
                }
            });
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            Interceptor header = new Interceptor() {

                @Override

                public Response intercept(Chain chain) throws IOException {

                    Request request = chain.request()

                            .newBuilder()

                            //                            .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                            //
                            //                            .addHeader("Accept-Encoding", "gzip, deflate")

                            .addHeader("Connection", "close")

                            //                            .addHeader("Accept", "*/*")
                            //
                            //                            .addHeader("Cookie", "add cookies here")

                            .build();

                    return chain.proceed(request);

                }


            };

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(connectTimeout, TimeUnit.SECONDS)
                    .readTimeout(readTimeout, TimeUnit.SECONDS)
                    .writeTimeout(writeTimeout, TimeUnit.SECONDS)
                    .sslSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory(), SSLSocketFactoryUtils.createTrustAllManager())
                    .hostnameVerifier(new SSLSocketFactoryUtils.TrustAllHostnameVerifier())
                    .addInterceptor(loggingInterceptor)
                    .addInterceptor(header)
                    .build();

            gsonRetrofit2 = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(ScalarsConverterFactory.create())//添加gson转换器
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
                    .client(okHttpClient)
                    .build();

        }
        return gsonRetrofit2;
    }

    public static <T> T createJsonService(Class<T> service) {
        return getJsonRetrofit().create(service);
    }
}
